<template>
<view>
<view class="welcome" v-if="showLoadFail">
  <image mode="widthFix" src="https://image1.sansancloud.com/zhihuishangwu/2019_11/21/19/57/47_607.jpg?x-oss-process=style/preview_120"></image>
  <button :disabled="showLoading" @tap="reloadJs" :loading="showLoading">重新加载</button>
</view>
<view v-else> 
</view>
</view>
</template>

<script>
var timer11; // 计时器
export default {
  data() {
    return {
      showLoadFail: false,
      showLoading: false,
      page_name: "",
      opt: {}
    };
  },
  components: {},
  props: {},
  onLoad: function (options) {
    console.log("=====options携带的参数=====", options);
    let that = this;
    getApp().globalData.shareSubPage = false;
    // #ifdef MP
    getApp().globalData.wxLogin(getApp().globalData.more_scene,0,0,function(){
        that.enterPage(options);
    });
    // #endif
    // #ifdef APP-PLUS ||H5
     getApp().globalData.getStorageFun("loginToken",function(res){
        console.log("==getStorageFun===res",res)
        let loginToken=''
        if(typeof(res)=="string"){
            loginToken=JSON.parse(res.data);
        }else{
            loginToken=res.data
        }
        getApp().globalData.removeStorageFun("cookie")
        getApp().globalData.loginToken=loginToken
        getApp().globalData.loginWithLoginToken(loginToken,function(){},function(){
            getApp().globalData.removeStorageFun("loginToken")
            getApp().globalData.loginToken='';
        },function(){
            that.enterPage(options);
        });
    },function(res){
        console.log("获取失败",res)
        getApp().globalData.wxLogin(getApp().globalData.more_scene,0,0,function(){
            that.enterPage(options);
        });
    })
    // #endif
    // setTimeout(function(){
    //     if (!getApp().globalData.setting.platformSetting.id) {
    //       console.log('还没获取到setting');
    //       getApp().globalData.getSetting().then(function (res) {
    //         console.log('已重新获取到setting');
    //         that.enterPage(options);
    //       });
    //     } else {
    //       console.log('已获取到setting');
    //       that.enterPage(options);
    //     }
    // },500)
  },
  onReady: function () {},
  onShow: function () {
    console.log("=========on show======", getApp().globalData.appHide, getApp().globalData.shareSubPage);
    if (getApp().globalData.appHide) {
      console.log("=======app.onLaunchOptions==========", getApp().globalData.onLaunchOptions);
      getApp().globalData.appHide = false; 
      // this.onReady()
    }
    if (getApp().globalData.shareSubPage) {
      console.log("========shareSubPage to index=====");
      getApp().globalData.shareSubPage = false;
      this.toIndex();
    }
  },
  methods: {
    toIndex: function () {
      let that = this;
      console.log("========app==", getApp().globalData.more_scene, that.page_name);
      if (that.page_name != "" && that.page_name != getApp().globalData.miniIndexPage + ".html") {
        var linkUrl = that.page_name;
        let urlData = getApp().globalData.getUrlParams(linkUrl);
        var resultUrl = linkUrl.substring(12, linkUrl.length - 5);
        if (urlData.param == '') {
          urlData.param = '?';
        }
        console.log("urlData" + urlData.param + "------" + resultUrl);
        setTimeout(function () {
          getApp().globalData.linkEvent('custom_page.html' + urlData.param + '&Cpage=' + resultUrl,'reLaunch');
          // wx.reLaunch({
          //   url: '/pages/custom_page/index' + urlData.param + '&Cpage=' + resultUrl
          // });
        }, 200);
      } else {
        console.log("==1=1=1=1=",getApp().globalData.miniIndexPage,getApp().globalData.miniIndexPageParams)
        if (getApp().globalData.miniIndexPage) {
          console.log("进入特定首页");
          let urlParam=getApp().globalData.miniIndexPage.slice(12);
          console.log("urlParam",urlParam);
          let url='custom_page_index.html?miniIndexPage='+urlParam
          // let url=getApp().globalData.miniIndexPage + (getApp().globalData.miniIndexPageParams?"."+getApp().globalData.miniIndexPageParams:".html")
          console.log("===url===",url)
          setTimeout(function () {
            getApp().globalData.linkEvent(url,'reLaunch');
          }, 200);
          return;
        } else {
          setTimeout(function () {
            getApp().globalData.linkEvent('custom_page_index.html','reLaunch');
            // wx.reLaunch({
            //   url: '/pageTab/' + 'custom_page_index' + '/index',
            //   fail: function () {
            //     console.log("==app.miniIndexPage==", getApp().globalData.miniIndexPage);
            //     getApp().globalData.linkEvent(getApp().globalData.miniIndexPage + '.html');
            //   }
            // });
          }, 200);
          return;
        }
      }
    },
    reloadJs: function () {
      //重新加載
      this.setData({
        showLoading: true
      });
      getApp().globalData.loadFirstEnter(getApp().globalData.more_scene);
      clearTimeout(timer11);
      this.count = 5;
      this.CountdownFun(app);
    },
    setNav: function () {
      wx.setNavigationBarTitle({
        title: '加载失败'
      });
    },
    enterPage: function (options) {
      let that = this;
      if (options.params && options.params != "") {
        let aaa = JSON.parse(options.params);
        console.log(aaa);
        options = aaa;
      }
      wx.getNetworkType({
        success: function (res) {
          if (res.networkType == 'none') {
            //无网络
            console.error('无网络');
            that.setNav();
            that.setData({
              showLoadFail: true
            });
          }
        }
      });
      if (options.RESOURCE_TYPE) {
        console.log("==========领取资源入口=========");
        let resourceType = options.RESOURCE_TYPE; //RESOURCE_TYPE

        let resourceId = options.RESOURCE_ID; //RESOURCE_ID

        let params = {
          resourceId: resourceId,
          resourceType: resourceType
        };
        params = getApp().globalData.jsonToStr2(params);
        let result = "resource_info.html?" + params;
        setTimeout(function () {
          getApp().globalData.goto(result);
        }, 200);
      }
      if (options.REDIRECT && options.REDIRECT != "") {
        getApp().globalData.linkEvent(options.REDIRECT);
        return;
      }
      if (options.ENTER_PLATFORM_NO && options.ENTER_PLATFORM_NO != "" && options.ENTER_PAGE_NAME && options.ENTER_PAGE_NAME != "") {
        console.log("options.page_name", options.ENTER_PAGE_NAME);
        that.setData({
          page_name: options.ENTER_PAGE_NAME
        });
        console.log(" app.clientNo", getApp().globalData.clientNo);
        getApp().globalData.clientNo = options.ENTER_PLATFORM_NO;
        getApp().globalData.getSetting();
        return;
      }
      if (!!options.scene && options.scene.indexOf('MINI_PLATFORM_USER_ID') != -1) {
        if (getApp().globalData.loginUser) {
          getApp().globalData.USER_DEFINED_SCENE = options.scene;
          getApp().globalData.changeUserBelong(options.scene);
        } else {
          getApp().globalData.USER_DEFINED_SCENE = options.scene;
        }
      } // 公用
      if ((options.SHARE_COMMON_PAGE && options.SHARE_COMMON_PAGE != "")||(options.COMMON_PAGE && options.COMMON_PAGE != "")) {
        console.log("=======SHARE_COMMON_PAGE=========", options);
        let linkUrl = options.linkUrl || 'custom_page_index';
        if (options.linkUrl) {
          delete options.linkUrl;
        }
        if (options.SHARE_COMMON_PAGE) {
          delete options.SHARE_COMMON_PAGE;
        }
        if (options.scene) {
          delete options.scene;
        }
        let params = getApp().globalData.jsonToStr2(options);
        console.log("=======params=========", params);
        let result = linkUrl + ".html?" + params;
        setTimeout(function () {
          getApp().globalData.goto(result);
        }, 200);
        return;
      }
       // 传入值携带桌子二维码的跳到订餐页面
      if (options.ENTER_ORDER_MEAL_TABLEID && options.ENTER_ORDER_MEAL_TABLEID != "" && options.ADDSHOPID && options.ADDSHOPID != "") {
        console.log("进入订餐页面", options.ENTER_ORDER_MEAL_TABLEID);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/order_meal/index?addShopId=' + options.ADDSHOPID+'&tableId='+options.ENTER_ORDER_MEAL_TABLEID,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200); // 缓存

        try {
          wx.setStorageSync('tableID', options.ENTER_ORDER_MEAL_TABLEID);
        } catch (e) {}

        return;
      }
       // 传入值携带桌子门店二维码的跳到门店订餐页面
      if (options.ENTER_ORDER_MEAL_TABLEID && options.ENTER_ORDER_MEAL_TABLEID != "" && options.MENDIANID && options.MENDIANID != "") {
        console.log("进入门店订餐页面", options.ENTER_ORDER_MEAL_TABLEID);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/order_meal_page/index?mendianId=' + options.MENDIANID+'&tableId='+options.ENTER_ORDER_MEAL_TABLEID+'&tableName='+options.TABLENAME,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200); // 缓存

        try {
          wx.setStorageSync('tableID', options.ENTER_ORDER_MEAL_TABLEID);
        } catch (e) {}

        return;
      }

      if (options.APPLY_SERVER_CHANNEL_CODE && options.APPLY_SERVER_CHANNEL_CODE != "") {
        console.log("进服务商申请页面", options.APPLY_SERVER_CHANNEL_CODE);
        let url = "";

        if (getApp().globalData.clientNo == "tunzai") {
          url = "/pageTab/tunzai/preApplyMendian/index";
        } else if (getApp().globalData.clientNo == "yunjishi") {
          url = "/pagesTwo/yunjishi/applyMendian/index?code=" + options.APPLY_SERVER_CHANNEL_CODE;
        } else {
          url = "/pagesTwo/yunjishi/applyMendian/index?code=" + options.APPLY_SERVER_CHANNEL_CODE;
        } //       applyMendian
        setTimeout(function () {
          wx.navigateTo({
            url: url + '?code=' + options.APPLY_SERVER_CHANNEL_CODE,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      }
      if (options.APPLY_SERVANT_CODE && options.APPLY_SERVANT_CODE != "") {
        console.log("进服务人员申请页面", options.APPLY_SERVANT_CODE); //       applyMendian
        setTimeout(function () {
          wx.navigateTo({
            url: '/pagesTwo/yunjishi/applyServant/index?reqType=2&code=' + options.APPLY_SERVANT_CODE,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      } //进入产品详情
      if (options.SHARE_PRODUCT_DETAIL_PAGE && options.SHARE_PRODUCT_DETAIL_PAGE != "") {
        console.log("options.SHARE_PRODUCT_DETAIL_PAGE", options.SHARE_PRODUCT_DETAIL_PAGE);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pagesTwo/productDetail/index?id=' + options.SHARE_PRODUCT_DETAIL_PAGE + "&addShopId=236",
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      } // 分享出来带分享SHARE_NEWS_DETAIL_PAGE跳到新闻详情页
      if (options.SHARE_NEWS_DETAIL_PAGE && options.SHARE_NEWS_DETAIL_PAGE != "") {
        console.log("进入新闻详情页面", options.SHARE_NEWS_DETAIL_PAGE);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/news_detail/index?id=' + options.SHARE_NEWS_DETAIL_PAGE,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      } // 分享出来带分享SHARE_FORM_DETAIL_PAGE跳到表单详情页
      if (options.SHARE_FORM_DETAIL_PAGE && options.SHARE_FORM_DETAIL_PAGE != "") {
        console.log("进入表单详情页面", options.SHARE_FORM_DETAIL_PAGE);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/form_detail/index?customFormId=' + options.SHARE_FORM_DETAIL_PAGE,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      } // 分享出来带分享SHARE_CHECK_FORM_DETAIL_PAGE跳到表单详情记录页
      if (options.SHARE_CHECK_FORM_DETAIL_PAGE && options.SHARE_CHECK_FORM_DETAIL_PAGE != "") {
        console.log("表单详情记录页", options.SHARE_CHECK_FORM_DETAIL_PAGE);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/check_form_detail/index?custom_form_commit_id=' + options.SHARE_CHECK_FORM_DETAIL_PAGE,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      } 
      // 分享出来带分享SHARE_CARD_VOUCHER_PAGE跳到卡券详情
      if (options.SHARE_CARD_VOUCHER_PAGE && options.SHARE_CARD_VOUCHER_PAGE != "") {
        console.log("卡券详情", options.SHARE_CARD_VOUCHER_PAGE);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/my_card_voucher_detail/index?id=' + options.SHARE_CARD_VOUCHER_PAGE,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      }
      // 分享出来带分享SHARE_USER_INFO_PAGE跳到我的页面
      if (options.SHARE_USER_INFO_PAGE && options.SHARE_USER_INFO_PAGE != "") {
        console.log("进入首页", options.SHARE_USER_INFO_PAGE);
        that.CountdownFun();
        return;
      } // 卡券扫码核销
      if (options.VERIFICATION_CODE && options.VERIFICATION_CODE != "") {
        console.log("进入卡券扫码核销页面", options);
        let params='code=' + options.VERIFICATION_CODE + "&verifyScanType=" + options.verifyScanType + "&verifySign=" + options.sign
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/verification_results/index?'+params,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      } // 拼团邀请
      if (options.PINTUAN_CODE && options.PINTUAN_CODE != "") {
        console.log("拼团邀请", options);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/pintuan_invitation/index?pintuanRecordId=' + options.PINTUAN_CODE,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      } // 分享出来带分享SHARE_PROMOTION_PRODUCTS_PAGE跳到产品详情页
      if (options.SHARE_PROMOTION_PRODUCTS_PAGE && options.SHARE_PROMOTION_PRODUCTS_PAGE != "") {
        console.log("进入特卖页面", options.SHARE_PROMOTION_PRODUCTS_PAGE);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pageTab/tunzai/teMai/index?promotionId=' + options.SHARE_PROMOTION_PRODUCTS_PAGE,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      } //如果传入的是 MendainID
      if (!!options.ENTER_MENDIAN && options.ENTER_MENDIAN != -1) {
        console.log("===================传入的门店====================id" + options.ENTER_MENDIAN);
        getApp().globalData.enterMenDianID = options.ENTER_MENDIAN;

        if (getApp().globalData.loginUser) {
          //已登陆需要再次重新登陆
          getApp().globalData.wxLogin(getApp().globalData.more_scene);
        }
      }
      let ENTER_SHOP = options.ENTER_SHOP;
      if (!!ENTER_SHOP) {
        console.log("ENTER_SHOP" + ENTER_SHOP);
        setTimeout(function () {
          wx.reLaunch({
            url: '/pagesTwo/near_shop_page/index?addShopId=' + ENTER_SHOP,
            success: function () {}
          });
        }, 200);
        return;
      } 
      let ENTER_MENDIAN_OFF_PAY = options.ENTER_MENDIAN_OFF_PAY;
      if (!!ENTER_MENDIAN_OFF_PAY) {
        console.log("ENTER_MENDIAN_OFF_PAY" + ENTER_MENDIAN_OFF_PAY);
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/new_pay_offline/index?id=' + ENTER_MENDIAN_OFF_PAY,
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
        return;
      } else if (getApp().globalData.setting && options.pageName && getApp().globalData.shareParam && getApp().globalData.shareParam.pageName) {
        setTimeout(function () {
          wx.navigateTo({
            url: '/pageTab/' + getApp().globalData.miniIndexPage + '/index',
            success: function () {
              getApp().globalData.shareSubPage = true;
            }
          });
        }, 200);
      } else {
        console.log('正常进入');
        that.CountdownFun();
      }
    },
    CountdownFun: function () {
      let that = this;
      --this.count;

      if (getApp().globalData.setting) {
        console.log("测试有走到这里index页面111行");
        clearTimeout(timer11);
        that.toIndex();
        return false;
      }

      if (this.count < 1) {
        getApp().globalData.echoErr('获取setting数据失败');
        this.setData({
          showLoadFail: true,
          showLoading: false
        });
        this.setNav();
        clearTimeout(timer11);
        return false;
      } else {
        timer11 = setTimeout(function () {
          that.CountdownFun();
        }, 1000);
      }
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>